package webviewdemo.printfuture.com.retrofitdownloadapk;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import zlc.season.rxdownload3.RxDownload;
import zlc.season.rxdownload3.core.Downloading;
import zlc.season.rxdownload3.core.Failed;
import zlc.season.rxdownload3.core.Normal;
import zlc.season.rxdownload3.core.Status;
import zlc.season.rxdownload3.core.Succeed;
import zlc.season.rxdownload3.core.Suspend;
import zlc.season.rxdownload3.core.Waiting;
import zlc.season.rxdownload3.extension.ApkInstallExtension;

public class MainActivity extends BaseActivity {

	private Button button_download_apk;
	private ProgressBar progress;
	private String TAG = "MainActivity";
	private Disposable disposable;
	private Status currentStatus = new Status();

	private static final String url = "http://center.paint-future.com/app/filemanager/file/download?key=447f0d330f0593351721650fffa4e7df";
	private TextView percent,size;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button_download_apk = (Button) findViewById(R.id.button_download_apk);
		progress = (ProgressBar) findViewById(R.id.progress);
		percent = (TextView)findViewById(R.id.percent);
		size = (TextView) findViewById(R.id.size);
		create();

		button_download_apk.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				downAPK();
			}
		});
	}

	private void create() {
		disposable = RxDownload.INSTANCE.create(url)
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Consumer<Status>() {
					@Override
					public void accept(Status status) throws Exception {
						currentStatus = status;
						setProgress(status);
						setActionText(status);
					}
				});
	}

	private void setProgress(Status status) {
		progress.setMax((int) status.getTotalSize());
		progress.setProgress((int) status.getDownloadSize());
		percent.setText(status.percent());
		size.setText(status.formatString());
	}

	private void downAPK() {
		if (currentStatus instanceof Normal) {
			start();
		} else if (currentStatus instanceof Suspend) {
			start();
		} else if (currentStatus instanceof Failed) {
			start();
		} else if (currentStatus instanceof Downloading) {
			stop();
		} else if (currentStatus instanceof Succeed) {
			install();
		} else if (currentStatus instanceof ApkInstallExtension.Installed) {
			open();
		}
	}

	private void setActionText(Status status) {
		String text = "";
		if (status instanceof Normal) {
			text = "开始";
		} else if (status instanceof Suspend) {
			text = "已暂停";
		} else if (status instanceof Waiting) {
			text = "等待中";
		} else if (status instanceof Downloading) {
			text = "暂停";
		} else if (status instanceof Failed) {
			text = "失败";
		} else if (status instanceof Succeed) {
			text = "安装";
		} else if (status instanceof ApkInstallExtension.Installing) {
			text = "安装中";
		} else if (status instanceof ApkInstallExtension.Installed) {
			text = "打开";
		}

		button_download_apk.setText(text);
	}


	private void start() {
		RxDownload.INSTANCE.start(url).subscribe();
	}

	private void stop() {
		RxDownload.INSTANCE.stop(url).subscribe();
	}
	private void install() {
		RxDownload.INSTANCE.extension(url, ApkInstallExtension.class).subscribe();
	}

	private void open() {
		//TODO: open app
	}

}
